﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class drug_category : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        //var result = from r in db.medical
        //             select r;
        //gvProduct.DataSource = result;
        //gvProduct.DataBind();
    }
    protected void drpselect_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvProduct.DataSourceID = "";
        string str = drpselect.SelectedValue;
        Label1.Text = str;
        var result = from r in db.medical
                     where r.MedClass == str
                     select r;
        gvProduct.DataSource = result;  //将LINQ查询结果设置为gvProduct的数据源
        gvProduct.DataBind();           //显示数据源中的数据

    }

    protected void gvProduct_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            try
            {
                LinkButton lnkbtnDelete = (LinkButton)e.Row.Cells[9].Controls[0];
                lnkbtnDelete.OnClientClick = "return confirm('删除药品" + e.Row.Cells[1].Text + "吗？')";
            }
            catch 
            { 
            }
        }
    }


    protected void gvProduct_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = gvProduct.SelectedRow;
        string selectedMedId = row.Cells[0].Text.Trim();
        string selectedMedPrice = row.Cells[3].Text.Trim();
        //Response.Write("<script>alert(" + selectedMedId + ");</script>");
        //Response.Write("<script>alert(" + selectedMedPrice + ");</script>");
        if (Request.Cookies == null)
        {
            return;
        }
        string userName = Request.Cookies["name"].Value;
        //string userName = "张三"; 
        var userId = from r in db.Users
                     where r.UserName == userName
                     select r.UserId;
        //Response.Write("<script>alert(" + userId.First().ToString().Trim() + ");</script>");
        var isExit = from r in db.trolley
                     where r.UserId == userId.First().ToString().Trim() && r.MedID == selectedMedId
                     select r;
        try
        {
            //修该原有行的数量
            isExit.First().ToString();
            foreach (trolley r in isExit)
            {
                r.MedCount = (int.Parse(r.MedCount) + 1).ToString();
            }
            db.SubmitChanges();

        }
        catch
        {
            //添加新行到购物车表
            trolley newTro = new trolley();
            newTro.TroID = getMaxId();
            //Response.Write("<script>alert(" + getMaxId() + ");</script>");
            newTro.MedCount = "1";
            newTro.MedPrice = selectedMedPrice;
            newTro.MedID = selectedMedId;
            newTro.UserId = userId.First().ToString().Trim();
            db.trolley.InsertOnSubmit(newTro);
            db.SubmitChanges();
        }
        finally {
            Response.Write("<script>alert('添加成功!');</script>");
        }
    }

    protected int getMaxId()
    {
        var results = from r in db.trolley
                      select r;
        int maxID = -1;
        if (results != null)
        {
            foreach (trolley r in results)
            {
                int id = r.TroID;
                if (id > maxID)
                {
                    maxID = id;
                }
            }
        }
        return maxID + 1;
    }
}